/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.model;

import java.net.MalformedURLException;
import java.util.HashMap;

/**
 *
 * @author Walter Lütgenau
 */
public class HttpUtils {
  /**
     * Add a parameter to the path and return the path.
     * 
     * @param path
     * @param key The key of the parameter
     * @param value The value of the parameter
     * @return The resulting path
     */
    public static String addParameter( String path, String key, String value ) {
        if ( path.indexOf("?") >= 0)
            path += "&" + key + "=" + value;
        else
            path += "?" + key + "=" + value;
        
        return path;
    }
    
    /**
     * Remove the parameter denoted by key from the url. Return the resulting url.
     * 
     * @param url The URL
     * @param key The key of the parameter to be removed
     * @return The resulting URL, without the parameter
     * @throws java.net.MalformedURLException
     */
    public static String removeParameter(String url, String key) throws MalformedURLException {
        int queryStart = url.indexOf("?");
        if (queryStart < 0)
            return url;
        
        String query = url.substring(queryStart+1);
        String[] parameters = query.split("&");
        StringBuffer buf = new StringBuffer(url.substring(0, queryStart+1));
        for( int i=0; i<parameters.length; i++) {
            if (!parameters[i].startsWith(key)) {
                buf.append(parameters[i]);
                buf.append("&");
            }
        }

        String result = buf.toString();
        if (result.endsWith("&"))
            result = result.substring(0,result.length() - 1);
        
        return result;
    }

    /**
     * Return the URL query parameters as a HashMap
     * 
     * @param url
     * @return
     */
    public static HashMap<String,String> getParameters(String url) {
        HashMap<String, String> result = new HashMap<String, String>();
        int startQuery = url.indexOf("?");
        if (startQuery < 0)
            return result;

        String query = url.substring(startQuery + 1);
        String[] parameterPairs = query.split("&");

        for (String parameterPair : parameterPairs) {
            String[] parameterParts = parameterPair.split("=");
            if (parameterParts.length == 2)
                result.put(parameterParts[0], parameterParts[1]);
        }

        return result;
    }

    public static void main(String[] arguments) {
        System.out.println(HttpUtils.getParameters("xxx?aa=aa&bb=bb"));
    }

}
